<?php

namespace app\admin\model\traits;

/*
 // 使用示例
public static function getForm($pleaseSelect=false, $isArrayIndex=false) {
    $list = self::getColumnAllCommon(false, ['title', 'id']);
    $tmp = [];
    foreach($list as $id => $title) {
        $tmp[$id] = sprintf("%s(%s)", $title, $id);
    }
    $list = $tmp;
    if($isArrayIndex) {
        return self::toArrayIndex($list, $pleaseSelect);
    }
    return self::toArrayAssoc($list, $pleaseSelect);
}
*/
trait ModelToForm
{

    // 转换为关联数组
    public static function toArrayAssoc($list, $pleaseSelect = false) {
        $data = [];
        if ($pleaseSelect !== false) {
            $data[''] = $pleaseSelect;
        }

        foreach ($list as $key => $val) {
            $data[$key] = $val;
        }
        return $data;
    }

    // 转换为索引数组
    public static function toArrayIndex($list, $pleaseSelect = false) {
        $data = [];

        if($pleaseSelect !== false) {
            $data[] = [
                'id' => '',
                'title' => $pleaseSelect,
            ];
        }

        foreach($list as $key => $val) {
            $data[] = [
                'id' => $key,
                'title' => $val,
            ];
        }

        return $data;
    }
}